import * as THREE from "three";
import { Coin } from "./Coin";
import { Config } from "./Config";
// import anime from 'animejs/lib/anime.es.js';
import { Tooler } from "./tooler";
import AnimeManager from "./AnimeManager";

export class Effect extends THREE.Object3D {
  coins: THREE.Object3D[] = [];
  total = 10;
  constructor() {
    super();
    this.init();
    console.log("new effect")
  }

  init(){
    const og = new THREE.OctahedronGeometry(4, 0);
    const mesh = new THREE.Mesh(og, new THREE.MeshBasicMaterial({color: 0xffffff}))
    for(let i = 0; i < this.total; i++){
      let coin = Config.getCoin()
      // console.log("coin", coin)
      this.coins.push(coin);
      this.add(coin);

      const box = mesh.clone();
      this.coins.push(box);
      this.add(box)
    }
  }

  play(){
    Config.sounds.playWin()
    this.coins.forEach(coin=>{
      coin.position.set(0, 0, 0)
      coin.rotation.set(0, 0, 0)
      coin.scale.set(1, 1, 1)
      AnimeManager.anime({
        targets: coin.position,
        x: Tooler.getRandomInt(-32, 32),
        y: Tooler.getRandomInt(-32, 32),
        z: Tooler.getRandomInt(-60, 60),
        duration: Config.CHOOSE_TIMER * 1.6,
        // easing: 'easeOutQuart'
      });
      AnimeManager.anime({
        targets: coin.rotation,
        x: Tooler.getRandomInt(2, 6),
        y: Tooler.getRandomInt(2, 6),
        z: Tooler.getRandomInt(2, 6),
        duration: Config.CHOOSE_TIMER * 1.6,
        // easing: 'easeOutQuart'
      });
      AnimeManager.anime({
        targets: coin.scale,
        x: 0.001,
        y: 0.001,
        z: 0.001,
        duration: Config.CHOOSE_TIMER * 1.6,
        easing: 'easeOutQuart'
      });
    })
  }

}